package ajax;

import Model.student;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/ajaxjson")
public class AjaxJson extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        req.setCharacterEncoding("UTF-8");
        ArrayList<student> students = new ArrayList<>();
        students.add(new student(12, "aa", 18, "180", "qh"));
        students.add(new student(13, "bb", 19, "180", "bd"));
        //java集合转换成json字符串
        String s = JSON.toJSONString(students);
        PrintWriter writer = resp.getWriter();
        writer.print(s);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
